<html>
<head><meta charset="utf-8"><title>Masks · project-portable-simd · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/index.html">project-portable-simd</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html">Masks</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236563994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236563994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236563994">(Apr 28 2021 at 19:24)</a>:</h4>
<p>Is there a way to use the masks in stdsimd? I see that I can generate them with comparisons, but I don't see any way that I can do conditional operations with them, or even mask things off. Perhaps I missed the intended use of them.<br>
In my case I'm a strong AVX-512 user so I'd want a way to express it that coerces the mask into the correct operation, but I'd suspect LLVM would be smart enough if there was a BitAnd implementation against SimdU and Masks.<br>
I think the ideal way to express would probably be <code>Simd::choose(mask, foo + bar, baz) -&gt; Simd</code>. I feel like years ago these was a choose (or otherwise named) API that allowed this. But I don't see it in the current iteration?</p>



<a name="236564641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236564641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236564641">(Apr 28 2021 at 19:29)</a>:</h4>
<p>There's  branch that offers selects, <span class="user-mention" data-user-id="356799">@Brandon Falk</span>.</p>



<a name="236564820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236564820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236564820">(Apr 28 2021 at 19:30)</a>:</h4>
<p>Ooh! That's super fresh. I'm gonna play around with the codegen for this a bit. Thanks! Anything I could do to help to push this to stabilization or testing? I do a lot of HPC on AVX-512 machines.</p>



<a name="236565016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236565016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236565016">(Apr 28 2021 at 19:32)</a>:</h4>
<p>By which I mean: there's a reason we haven't actually published the crate. :D There's still a bit more in-progress than we'd like to try to launch with.<br>
I also would like to offer my sympathies to you as a future prospective user of the std::simd mask API as we are definitely going to break this several times on nightly, heh.</p>



<a name="236565136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236565136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236565136">(Apr 28 2021 at 19:33)</a>:</h4>
<p>It's become something of a meme that we keep rewriting the masks design at this point.</p>



<a name="236565385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236565385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236565385">(Apr 28 2021 at 19:34)</a>:</h4>
<p>all for good reasons ofc <span aria-label="like" class="emoji emoji-1f44d" role="img" title="like">:like:</span></p>



<a name="236565400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236565400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236565400">(Apr 28 2021 at 19:34)</a>:</h4>
<p>I probably rewrite my core code more frequently than this will change so I'm not too worred. I used stdsimd with select ~2-3 years ago and it was really amazing. For most of my day-to-day projects select is the only feature I'm missing. But for some of my more complex projects I'll definitely need scatter gather support and the like.</p>



<a name="236565664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236565664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236565664">(Apr 28 2021 at 19:36)</a>:</h4>
<p>As long as I have select, a way to do signed/unsigned comparisons into masks, and support for all variants of AVX-512 lanes (u8 through u64, with 512-bit vectors) I can express pretty much everything I need. LLVM codegen looks good enough at creating ternlogs and FMAs that I don't need to directly express those. I wrote an MD5 implementation in stdsimd recently and it correctly coerced everything to ternlog and was at parity with my hand-written assembly! I absolutely love this stuff. Great part is the code is still usable on some random architecture if I absolutely need it to run somewhere else.<br>
(for people who care: <a href="https://gist.github.com/gamozolabs/a3940b28ced4dcfeafb94b11632307c5">https://gist.github.com/gamozolabs/a3940b28ced4dcfeafb94b11632307c5</a>)</p>



<a name="236565728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236565728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236565728">(Apr 28 2021 at 19:37)</a>:</h4>
<p>Yes, for good reasons, just...!<br>
And ahh, this is basically a different crate now. :D<br>
Some... reorganization of SIMD stuff happened.</p>
<p>There's <code>std::arch</code>, there will be <code>std::simd</code> on nightly when we get all the nightly feature guards into our code and launched into rust-lang/rust, and there was <code>packed_simd</code>, now <code>packed_simd_2</code>.</p>



<a name="236565777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236565777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236565777">(Apr 28 2021 at 19:37)</a>:</h4>
<p>We've been working on this for the past ~6 months or so.</p>



<a name="236566005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236566005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236566005">(Apr 28 2021 at 19:39)</a>:</h4>
<p>Oh nice, that is definitely using <code>core_simd</code>, cool.</p>



<a name="236566011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236566011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236566011">(Apr 28 2021 at 19:39)</a>:</h4>
<p>Gonna write some of my... very... important WoW theorycrafting tools in simd + select branch, I'll keep an eye on how the codegen looks. I suspect since LLVM can convert some pretty convoluted manual bitmasking into the correct mask instructions, it'll easily do it with select.</p>



<a name="236566173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236566173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236566173">(Apr 28 2021 at 19:40)</a>:</h4>
<p>the only select instruction I can recall off the top of my head is <code>setvl</code></p>



<a name="236566205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236566205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236566205">(Apr 28 2021 at 19:40)</a>:</h4>
<p>I'm not positive yet, but I'm pretty sure select is the only operation we need to implement for masks</p>



<a name="236566220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236566220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236566220">(Apr 28 2021 at 19:40)</a>:</h4>
<p>I love seeing that there's horizontal adds, maxes, etc. I always manually implement that. Rotates might be nice (probably in some branch). I had no idea this was so actively developed that I should probably poke around branches.</p>



<a name="236566273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236566273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236566273">(Apr 28 2021 at 19:40)</a>:</h4>
<p>Part of our issue has been "how to handle masks and how much we trust LLVM codegen to paper over architectural differences" so your continued feedback on this and just "yeah, this is what I expect good codegen to look like <code>${snippet_of_disasm}</code>" is good.</p>



<a name="236566279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236566279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236566279">(Apr 28 2021 at 19:41)</a>:</h4>
<p>AVX-512 has a million mask instructions but they all seem to boil down to select + another operation</p>



<a name="236566281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236566281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236566281">(Apr 28 2021 at 19:41)</a>:</h4>
<p>I would suspect it should be, and should also have some pretty clean code <code>(a + b).select(mask, a - b)</code> sort of thing. I'd expect LLVM to correctly emit a <code>vpadd</code> and <code>vpsub</code> instruction with inverted masks. Or more specifically, one unconditonal, and one conditional.</p>



<a name="236566381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236566381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236566381">(Apr 28 2021 at 19:41)</a>:</h4>
<p>In my branch right now it would be <code>mask.select(a + b, a - b)</code> because of some trait magic!</p>



<a name="236566481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236566481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236566481">(Apr 28 2021 at 19:42)</a>:</h4>
<p>Y'all are amazing!! &lt;3</p>



<a name="236567699"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236567699" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236567699">(Apr 28 2021 at 19:51)</a>:</h4>
<p>Awhhh. Definitely no coercing to what it should by LLVM :( <a href="https://gist.github.com/gamozolabs/ad053c98707017d33e8b81f6227d374a">https://gist.github.com/gamozolabs/ad053c98707017d33e8b81f6227d374a</a></p>



<a name="236567989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236567989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236567989">(Apr 28 2021 at 19:53)</a>:</h4>
<p>This is AVX-512 right? I'm actually not surprised</p>



<a name="236568078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568078">(Apr 28 2021 at 19:54)</a>:</h4>
<p>Yeah. It should be able to generate 2 instructions. The old stdsimd from ~2017 or 2018 would generate correctly. Not sure how they implemented it, or if LLVM changed</p>



<a name="236568086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568086">(Apr 28 2021 at 19:54)</a>:</h4>
<p>I have a pretty gross hack for architectures that use compressed bit masks that I think I'm seeing there</p>



<a name="236568119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568119">(Apr 28 2021 at 19:54)</a>:</h4>
<p>Because rustc is currently missing an intrinsic I need.</p>



<a name="236568144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568144">(Apr 28 2021 at 19:54)</a>:</h4>
<p>Once I add that I believe it should work as expected</p>



<a name="236568272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568272">(Apr 28 2021 at 19:55)</a>:</h4>
<p>What's the hack? I could try it out if I could kludge it in quick. I've got a relatively large project I plan to write today using select so it'd be a good stress test</p>



<a name="236568279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568279">(Apr 28 2021 at 19:55)</a>:</h4>
<p>Alternatively I could just change the default AVX-512 mask representation and it would probably work</p>



<a name="236568394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568394">(Apr 28 2021 at 19:56)</a>:</h4>
<p><code>MaskX</code> are I'd imagine <code>[0xff, 0x0, 0xff]</code> sorts of things? Where <code>BitMask</code> is the AVX-512 style?</p>



<a name="236568399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568399">(Apr 28 2021 at 19:56)</a>:</h4>
<p>So unlike packed_simd I use the k-register style mask instead of the SSE/AVX style</p>



<a name="236568492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568492">(Apr 28 2021 at 19:57)</a>:</h4>
<p>But because of the way our intrinsics work it needs to do a sign extension up to the lane width to actually use the mask, but we don't have a sign extension intrinsic yet</p>



<a name="236568497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568497">(Apr 28 2021 at 19:57)</a>:</h4>
<p>I see, that's why it's getting shifted over I'd imagine. LLVM I think can figure out how to generates k masks from masked operations</p>



<a name="236568529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568529">(Apr 28 2021 at 19:57)</a>:</h4>
<p>So what you are seeing is LLVM can't see through my manual sign extension</p>



<a name="236568566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568566">(Apr 28 2021 at 19:57)</a>:</h4>
<p>Exactly</p>



<a name="236568724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568724">(Apr 28 2021 at 19:58)</a>:</h4>
<p>Where's the sign extension done? I see the call to the select intrinsic.</p>



<a name="236568977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236568977">(Apr 28 2021 at 20:00)</a>:</h4>
<p><a href="https://github.com/rust-lang/stdsimd/blob/676956d1c650e8b27d3e9311af6ae3d28c3ba327/crates/core_simd/src/masks/bitmask.rs#L33">https://github.com/rust-lang/stdsimd/blob/676956d1c650e8b27d3e9311af6ae3d28c3ba327/crates/core_simd/src/masks/bitmask.rs#L33</a></p>



<a name="236569149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236569149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236569149">(Apr 28 2021 at 20:01)</a>:</h4>
<p>I see, so this takes in a mask int 0x5 and produces <code>[0xf, 0, 0xf]</code> for a 3-vector of 4-bit values</p>



<a name="236569175"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236569175" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236569175">(Apr 28 2021 at 20:01)</a>:</h4>
<p>Yeah exactly</p>



<a name="236569242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236569242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236569242">(Apr 28 2021 at 20:02)</a>:</h4>
<p>It should just use the LLVM <code>sext</code> instruction to do that, which would allow optimization</p>



<a name="236569501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236569501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236569501">(Apr 28 2021 at 20:03)</a>:</h4>
<p>Select directly generates the LLVM simd select which expects the mask format? It's not generic over kmask-style?</p>



<a name="236570356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236570356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236570356">(Apr 28 2021 at 20:10)</a>:</h4>
<p>LLVM select takes &lt;i1 x N&gt; which doesn't really have a particular layout.  But the rustc intrinsics take &lt;iM x N&gt; where M is the bit width of the lane</p>



<a name="236570538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236570538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236570538">(Apr 28 2021 at 20:11)</a>:</h4>
<p>We could expose a whole secondary set of intrinsics that use a single integer as a k-style mask, but it would be easier to just expose a function that sign extends</p>



<a name="236570585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236570585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236570585">(Apr 28 2021 at 20:12)</a>:</h4>
<p>And let LLVM optimize out the back-to-back sign extension and truncation</p>



<a name="236572024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236572024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236572024">(Apr 28 2021 at 20:21)</a>:</h4>
<p><span class="user-mention silent" data-user-id="312331">Caleb Zulawski</span> <a href="#narrow/stream/257879-project-portable-simd/topic/Masks/near/236568119">said</a>:</p>
<blockquote>
<p>Because rustc is currently missing an intrinsic I need.</p>
</blockquote>
<p>what intrinsic are you missing?</p>



<a name="236572071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236572071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236572071">(Apr 28 2021 at 20:22)</a>:</h4>
<p><code>simd_sext</code>?</p>



<a name="236572145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236572145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236572145">(Apr 28 2021 at 20:22)</a>:</h4>
<p>OK~</p>



<a name="236572172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236572172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236572172">(Apr 28 2021 at 20:22)</a>:</h4>
<p>It's a little more specific than that. Right now we have <code>simd_bitmask</code>, which takes a mask and creates an integer</p>



<a name="236572191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236572191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236572191">(Apr 28 2021 at 20:22)</a>:</h4>
<p>We need something that takes an integer and creates a mask</p>



<a name="236572231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236572231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236572231">(Apr 28 2021 at 20:23)</a>:</h4>
<p>So it's a <code>bitcast</code> followed by a <code>sext</code></p>



<a name="236572244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236572244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236572244">(Apr 28 2021 at 20:23)</a>:</h4>
<p>ahh OK.</p>



<a name="236572312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236572312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236572312">(Apr 28 2021 at 20:23)</a>:</h4>
<p><code>simd_expand_bitmask</code>? Idk</p>



<a name="236572667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236572667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236572667">(Apr 28 2021 at 20:26)</a>:</h4>
<p>Hmm I might be able to persuade it to do sext properly without an intrinsic.<br>
But I noted it in <a href="https://github.com/rust-lang/stdsimd/issues/46">https://github.com/rust-lang/stdsimd/issues/46</a> anyways</p>



<a name="236572735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236572735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236572735">(Apr 28 2021 at 20:26)</a>:</h4>
<p>I don't think it's possible because we don't have any i1 type in rust</p>



<a name="236574219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236574219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236574219">(Apr 28 2021 at 20:38)</a>:</h4>
<p>Hmm, I think it's the loop or just, the multiple sets it can't get past. Might be really hard without a kmask simd select intrinsic</p>



<a name="236574311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236574311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236574311">(Apr 28 2021 at 20:38)</a>:</h4>
<p>yeah internally LLVM uses an <code>i1</code> type for a lot of things like this.</p>



<a name="236583253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236583253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236583253">(Apr 28 2021 at 21:24)</a>:</h4>
<p>Hmm, I'm scared that LLVM is not quite understanding the bit extraction. I wrote a different version with much better code generation, but it's still bad.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">mask</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">&lt;</span><span class="k">crate</span>::<span class="n">SimdI32</span><span class="o">&lt;</span><span class="mi">16</span><span class="o">&gt;&gt;</span>::<span class="n">from_array</span><span class="p">([</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">0</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">2</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">3</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">4</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">5</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">6</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">7</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">8</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">9</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">10</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">11</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">12</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">13</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">14</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">test</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">15</span><span class="p">))</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">31</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">])};</span><span class="w"></span>
</code></pre></div>
<div class="codehilite" data-code-language="objdump-nasm"><pre><span></span><code><span class="mh">0000000000000000</span> <span class="p">&lt;</span><span class="nf">stdsimd_test::add_or_sub</span><span class="p">&gt;:</span>
<span class="x">   0:   48 81 ec b8 00 00 00    sub    rsp,0xb8</span>
<span class="x">   7:   62 f1 fe 48 7f 0c 24    vmovdqu64 ZMMWORD PTR [rsp],zmm1</span>
<span class="x">   e:   62 f1 7c 48 11 44 24    vmovups ZMMWORD PTR [rsp+0x40],zmm0</span>
<span class="x">  15:   01</span>
<span class="x">  16:   c5 f8 77                vzeroupper</span>
<span class="x">  19:   e8 00 00 00 00          call   1e &lt;stdsimd_test::add_or_sub+0x1e&gt;</span>
<span class="x">  1e:   62 f2 7d 48 7c c0       vpbroadcastd zmm0,eax</span>
<span class="x">  24:   62 f2 7d 48 47 05 00    vpsllvd zmm0,zmm0,ZMMWORD PTR [rip+0x0]        # 2e &lt;stdsimd_test::add_or_sub+0x2e&gt;</span>
<span class="x">  2b:   00 00 00</span>
<span class="x">  2e:   62 f3 75 48 25 c9 ff    vpternlogd zmm1,zmm1,zmm1,0xff</span>
<span class="x">  35:   62 f1 7d 48 66 c9       vpcmpgtd k1,zmm0,zmm1</span>
<span class="x">  3b:   c5 f9 ef c0             vpxor  xmm0,xmm0,xmm0</span>
<span class="x">  3f:   62 f1 fe 48 6f 0c 24    vmovdqu64 zmm1,ZMMWORD PTR [rsp]</span>
<span class="x">  46:   62 f1 7d 49 fa c9       vpsubd zmm1{k1},zmm0,zmm1</span>
<span class="x">  4c:   62 f1 75 48 fe 44 24    vpaddd zmm0,zmm1,ZMMWORD PTR [rsp+0x40]</span>
<span class="x">  53:   01</span>
<span class="x">  54:   48 81 c4 b8 00 00 00    add    rsp,0xb8</span>
<span class="x">  5b:   c3                      ret</span>
</code></pre></div>



<a name="236583337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236583337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236583337">(Apr 28 2021 at 21:24)</a>:</h4>
<p>It effectively now is a broadcast of the mask into a register, then a parallel sign extension of each lane... lol</p>



<a name="236583487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236583487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236583487">(Apr 28 2021 at 21:25)</a>:</h4>
<p>There really is no workaround until we get the sign extension intrinsic into rustc (which isn't really a big deal)</p>



<a name="236583615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236583615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236583615">(Apr 28 2021 at 21:26)</a>:</h4>
<p>Would LLVM understand the bit extraction at that point? Because I think for these levels of unrolling it starts being a bit unreasonable. I'm a bit afraid that the issue is not just the sign extension, but also the bit extraction.</p>



<a name="236583704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236583704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236583704">(Apr 28 2021 at 21:26)</a>:</h4>
<p>LLVM will understand it because it won't be a "bit extraction" it will be a conversion of i1 to i16 and back to i1, which is trivial to optimize</p>



<a name="236583773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236583773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236583773">(Apr 28 2021 at 21:27)</a>:</h4>
<p>A relatively simply (but a bit boilerplatey) workaround for now would be to use <code>_m512_mask_set1_epi32(mask, !0)</code> to convert a mask into a vector of i32s</p>



<a name="236584891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236584891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236584891">(Apr 28 2021 at 21:35)</a>:</h4>
<p>Wait a minute, am I missing something really obvious?</p>



<a name="236584955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236584955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236584955">(Apr 28 2021 at 21:35)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="c1">// select</span>
<span class="w">    </span><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="k">fn</span> <span class="nf">simd_select</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="p">(</span><span class="n">m</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="n">a</span>: <span class="nc">U</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="nc">U</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">U</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="k">fn</span> <span class="nf">simd_select_bitmask</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="p">(</span><span class="n">m</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="n">a</span>: <span class="nc">U</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="nc">U</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">U</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="cp">#[inline]</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">select</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">true_values</span>: <span class="nc">crate</span>::<span class="n">SimdU32</span><span class="o">&lt;</span><span class="mi">16</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">false_values</span>: <span class="nc">crate</span>::<span class="n">SimdU32</span><span class="o">&lt;</span><span class="mi">16</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">crate</span>::<span class="n">SimdU32</span><span class="o">&lt;</span><span class="mi">16</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">use</span><span class="w"> </span><span class="k">crate</span>::<span class="n">masks</span>::<span class="n">ToBitMask</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">crate</span>::<span class="n">intrinsics</span>::<span class="n">simd_select_bitmask</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">to_bitmask</span><span class="p">()</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u16</span><span class="p">,</span><span class="w"> </span><span class="n">true_values</span><span class="p">,</span><span class="w"> </span><span class="n">false_values</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This works on <code>rustc 1.53.0-nightly (42816d61e 2021-04-24)</code><br>
Idk how new this intrinsic is, but there's just a straight up <code>simd_select_bitmask</code> intrinsic which is intererpreted perfectly by LLVM.</p>
<div class="codehilite" data-code-language="objdump-nasm"><pre><span></span><code><span class="mh">0000000000000000</span> <span class="p">&lt;</span><span class="nf">stdsimd_test::add_or_sub</span><span class="p">&gt;:</span>
<span class="x">   0:   48 81 ec b8 00 00 00    sub    rsp,0xb8</span>
<span class="x">   7:   62 f1 7c 48 11 0c 24    vmovups ZMMWORD PTR [rsp],zmm1</span>
<span class="x">   e:   62 f1 fe 48 7f 44 24    vmovdqu64 ZMMWORD PTR [rsp+0x40],zmm0</span>
<span class="x">  15:   01</span>
<span class="x">  16:   c5 f8 77                vzeroupper</span>
<span class="x">  19:   e8 00 00 00 00          call   1e &lt;stdsimd_test::add_or_sub+0x1e&gt;</span>
<span class="x">  1e:   c5 fb 92 c8             kmovd  k1,eax</span>
<span class="x">  22:   c5 f9 ef c0             vpxor  xmm0,xmm0,xmm0</span>
<span class="x">  26:   62 f1 fe 48 6f 0c 24    vmovdqu64 zmm1,ZMMWORD PTR [rsp]</span>
<span class="x">  2d:   62 f1 7d 48 fa c1       vpsubd zmm0,zmm0,zmm1</span>
<span class="x">  33:   62 f1 7d 49 6f c1       vmovdqa32 zmm0{k1},zmm1</span>
<span class="x">  39:   62 f1 7d 48 fe 44 24    vpaddd zmm0,zmm0,ZMMWORD PTR [rsp+0x40]</span>
<span class="x">  40:   01</span>
<span class="x">  41:   48 81 c4 b8 00 00 00    add    rsp,0xb8</span>
<span class="x">  48:   c3                      ret</span>
</code></pre></div>



<a name="236585327"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236585327" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236585327">(Apr 28 2021 at 21:39)</a>:</h4>
<p>That's a good point, there is that one, we might be able to use it in the meantime, though I don't personally want to use it to perform the select in the end</p>



<a name="236585494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236585494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236585494">(Apr 28 2021 at 21:40)</a>:</h4>
<p>Just for code cleanliness/uniformity?</p>



<a name="236585595"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236585595" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236585595">(Apr 28 2021 at 21:42)</a>:</h4>
<p>Yeah, more or less.  Though I'd have to look to see what else we actually use masks for, it might actually be fine.</p>



<a name="236585707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236585707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236585707">(Apr 28 2021 at 21:43)</a>:</h4>
<p>Optionally it could be used in only one place for <code>to_int()</code> and it could be used to bitmask select between !0 and 0, generating the standard SSE-style mask. I would imagine LLVM would trivially optimize that. I'm gonna test it</p>



<a name="236586645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236586645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236586645">(Apr 28 2021 at 21:52)</a>:</h4>
<p>Wow, yeah, look at this codegen.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="cp">#[inline]</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">to_int</span><span class="o">&lt;</span><span class="n">V</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">V</span><span class="w"></span>
<span class="w">    </span><span class="k">where</span><span class="w"></span>
<span class="w">        </span><span class="n">V</span>: <span class="nb">Default</span> <span class="o">+</span><span class="w"> </span><span class="nb">AsMut</span><span class="o">&lt;</span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">LANES</span><span class="p">]</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">T</span>: <span class="nb">From</span><span class="o">&lt;</span><span class="kt">i8</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">ones</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">V</span>::<span class="n">default</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="n">ones</span><span class="p">.</span><span class="n">as_mut</span><span class="p">().</span><span class="n">iter_mut</span><span class="p">().</span><span class="n">for_each</span><span class="p">(</span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span>::<span class="n">from</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">));</span><span class="w"></span>

<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">crate</span>::<span class="n">intrinsics</span>::<span class="n">simd_select_bitmask</span><span class="p">(</span><span class="w"></span>
<span class="w">                </span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u16</span><span class="p">,</span><span class="w"> </span><span class="n">ones</span><span class="p">,</span><span class="w"> </span><span class="n">V</span>::<span class="n">default</span><span class="p">())</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite" data-code-language="objdump-nasm"><pre><span></span><code><span class="mh">0000000000000000</span> <span class="p">&lt;</span><span class="nf">stdsimd_test::add_or_sub</span><span class="p">&gt;:</span>
<span class="x">   0:   c5 fb 92 cf             kmovd  k1,edi</span>
<span class="x">   4:   c5 e9 ef d2             vpxor  xmm2,xmm2,xmm2</span>
<span class="x">   8:   62 f1 6d 48 fa d1       vpsubd zmm2,zmm2,zmm1</span>
<span class="x">   e:   62 f1 7d 49 6f d1       vmovdqa32 zmm2{k1},zmm1</span>
<span class="x">  14:   62 f1 6d 48 fe c0       vpaddd zmm0,zmm2,zmm0</span>
<span class="x">  1a:   c3                      ret</span>
</code></pre></div>
<p>Gonna be hard to beat that!</p>



<a name="236586735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236586735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236586735">(Apr 28 2021 at 21:53)</a>:</h4>
<p>Good call on using <code>simd_select_bitmask</code>, that's basically what the intrinsic needs to do (the intrinsic will be slightly cleaner, but this is a good stopgap)</p>



<a name="236586838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236586838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236586838">(Apr 28 2021 at 21:54)</a>:</h4>
<p>The only hard part will be plumbing the <code>u16</code>, as <code>simd_select_bitmask</code> has strict width requirements that must match the vector, and the <code>self.0</code> must be cast to it. The current code doesn't have the information I don't think (this would compiler panic outside of my <code>u32x16</code> test, as the <code>u16</code> only matches <code>XXXx16</code> right now</p>



<a name="236587009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236587009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236587009">(Apr 28 2021 at 21:56)</a>:</h4>
<p>I just now realize that when you mean sign extend, you mean the vectorized version. For some reason I thought you meant a scalar sign extension intrinsic, combined with the loop that was used prior in <code>to_int</code>. The loop and scalar shifts was where I was scared.</p>



<a name="236587100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236587100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236587100">(Apr 28 2021 at 21:57)</a>:</h4>
<p>Oh gotcha, yeah, definitely on vectors</p>



<a name="236587224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236587224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236587224">(Apr 28 2021 at 21:58)</a>:</h4>
<p>How would you hazard I best get the <code>u&lt;LANES&gt;</code> type for this function?</p>



<a name="236587473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236587473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236587473">(Apr 28 2021 at 22:00)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">        </span><span class="n">require</span><span class="o">!</span><span class="p">(</span><span class="w"></span>
<span class="w">            </span><span class="c1">// Allow masks for vectors with fewer than 8 elements to be</span>
<span class="w">            </span><span class="c1">// represented with a u8 or i8.</span>
<span class="w">            </span><span class="n">m_len</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">v_len</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="p">(</span><span class="n">m_len</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">v_len</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">8</span><span class="p">),</span><span class="w"></span>
<span class="w">            </span><span class="s">"mismatched lengths: mask length `{}` != other vector length `{}`"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">m_len</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">v_len</span><span class="w"></span>
<span class="w">        </span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>Looks like we need to provide a bitmask of &lt;=8 bits for &lt;=LANES, but for all other LANES it must be _exact_</p>



<a name="236588313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236588313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236588313">(Apr 28 2021 at 22:08)</a>:</h4>
<p>Yeah, that's actually a bit of a problem now that I think about it</p>



<a name="236588366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236588366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236588366">(Apr 28 2021 at 22:09)</a>:</h4>
<p>Part of the problem is that rustc is unnecessarily strict</p>



<a name="236588867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236588867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236588867">(Apr 28 2021 at 22:14)</a>:</h4>
<p>about what?</p>



<a name="236588913"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236588913" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236588913">(Apr 28 2021 at 22:14)</a>:</h4>
<p>requiring the bitmask to be the exact same width as the number of lanes</p>



<a name="236588925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236588925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236588925">(Apr 28 2021 at 22:14)</a>:</h4>
<p>ahh.</p>



<a name="236588962"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236588962" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236588962">(Apr 28 2021 at 22:15)</a>:</h4>
<p>but also, there's the overarching problem of not being able to specify the bitmask type generically, anyway</p>



<a name="236588989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236588989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236588989">(Apr 28 2021 at 22:15)</a>:</h4>
<p>right.</p>



<a name="236590270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236590270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236590270">(Apr 28 2021 at 22:28)</a>:</h4>
<p>okay I think I can go back and change how <code>ToBitMask</code> is implemented to fix all of this</p>



<a name="236590279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236590279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236590279">(Apr 28 2021 at 22:28)</a>:</h4>
<p>ahaha</p>



<a name="236590288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236590288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236590288">(Apr 28 2021 at 22:29)</a>:</h4>
<p>Oooo</p>



<a name="236590333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236590333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236590333">(Apr 28 2021 at 22:29)</a>:</h4>
<p>(sorry for the big bother, but I have a project I wanted to start today and I realllllly want to be done writing AVX-512 specific code, as it's not the most user-friendly :D)</p>



<a name="236590405"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236590405" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236590405">(Apr 28 2021 at 22:30)</a>:</h4>
<p>and if we change the publicly exposed bitmasks to arrays of <code>u8</code>s we can potentially keep this "stable" by changing the implementation in the long term to implement over all bit masks and not just our select few</p>



<a name="236590426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236590426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236590426">(Apr 28 2021 at 22:30)</a>:</h4>
<p>if const generics makes progress in that direction</p>



<a name="236591123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236591123">(Apr 28 2021 at 22:37)</a>:</h4>
<p>I have a specialized implementation if you want me to PR it. Kinda nasty though</p>



<a name="236591125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236591125">(Apr 28 2021 at 22:37)</a>:</h4>
<p>So what exactly are you thinking of doing, Caleb?</p>



<a name="236591225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236591225">(Apr 28 2021 at 22:38)</a>:</h4>
<p><span class="user-mention silent" data-user-id="356799">Brandon Falk</span> <a href="#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591123">said</a>:</p>
<blockquote>
<p>I have a specialized implementation if you want me to PR it. Kinda nasty though</p>
</blockquote>
<p>Oh?</p>



<a name="236591244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236591244">(Apr 28 2021 at 22:38)</a>:</h4>
<p>Added <code>to_int()</code> and <code>select()</code> specialization for 2, 4, 8, 16, 32</p>



<a name="236591266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236591266">(Apr 28 2021 at 22:39)</a>:</h4>
<p>Well let me play around a bit, I want to perhaps try to work this into the mask PR</p>



<a name="236591294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236591294">(Apr 28 2021 at 22:39)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">macro_rules!</span><span class="w"> </span><span class="n">impl_to_int</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="cp">$bits</span>:<span class="nc">expr</span><span class="p">,</span><span class="w"> </span><span class="cp">$vecmaskty</span>:<span class="nc">ty</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">impl</span><span class="w"> </span><span class="n">BitMask</span><span class="o">&lt;</span><span class="cp">$bits</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="cp">#[inline]</span><span class="w"></span>
<span class="w">            </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">to_int</span><span class="o">&lt;</span><span class="n">V</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">V</span><span class="w"></span>
<span class="w">            </span><span class="k">where</span><span class="w"></span>
<span class="w">                </span><span class="n">V</span>: <span class="nb">Default</span> <span class="o">+</span><span class="w"> </span><span class="nb">AsMut</span><span class="o">&lt;</span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="cp">$bits</span><span class="p">]</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="n">T</span>: <span class="nb">From</span><span class="o">&lt;</span><span class="kt">i8</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">ones</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">V</span>::<span class="n">default</span><span class="p">();</span><span class="w"></span>
<span class="w">                </span><span class="n">ones</span><span class="p">.</span><span class="n">as_mut</span><span class="p">().</span><span class="n">iter_mut</span><span class="p">().</span><span class="n">for_each</span><span class="p">(</span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span>::<span class="n">from</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">));</span><span class="w"></span>

<span class="w">                </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="k">crate</span>::<span class="n">intrinsics</span>::<span class="n">simd_select_bitmask</span><span class="p">(</span><span class="w"></span>
<span class="w">                        </span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="cp">$vecmaskty</span><span class="p">,</span><span class="w"> </span><span class="n">ones</span><span class="p">,</span><span class="w"> </span><span class="n">V</span>::<span class="n">default</span><span class="p">())</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">impl_to_int</span><span class="o">!</span><span class="p">(</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">);</span><span class="w"></span>
<span class="n">impl_to_int</span><span class="o">!</span><span class="p">(</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">);</span><span class="w"></span>
<span class="n">impl_to_int</span><span class="o">!</span><span class="p">(</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">);</span><span class="w"></span>
<span class="n">impl_to_int</span><span class="o">!</span><span class="p">(</span><span class="mi">16</span><span class="p">,</span><span class="w"> </span><span class="kt">u16</span><span class="p">);</span><span class="w"></span>
<span class="n">impl_to_int</span><span class="o">!</span><span class="p">(</span><span class="mi">32</span><span class="p">,</span><span class="w"> </span><span class="kt">u32</span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>It's this at the core, and then this bubbles up to <code>select()</code> and <code>to_int()</code> which have to be specialized to invoke these. I used macros at each level</p>



<a name="236591295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236591295">(Apr 28 2021 at 22:39)</a>:</h4>
<p>I was definitely hoping to avoid specializing select (and was thinking about doing the same for shuffle)</p>



<a name="236591301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236591301">(Apr 28 2021 at 22:39)</a>:</h4>
<p>For sure!</p>



<a name="236591421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236591421">(Apr 28 2021 at 22:40)</a>:</h4>
<p>Oh yeah, also having select marked <code>#[inline]</code> helped a bit during some tests, which it currently is not. I had the code hacked up a lot when I was doing early testing and it was necessary to avoid a call to select. Since we implement it directly on the <code>$mask</code> I think it's necessary.</p>



<a name="236591547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236591547">(Apr 28 2021 at 22:42)</a>:</h4>
<p>Whoops that's definitely a bug</p>



<a name="236592448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236592448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236592448">(Apr 28 2021 at 22:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="356799">Brandon Falk</span> <a href="#narrow/stream/257879-project-portable-simd/topic/Masks/near/236591421">said</a>:</p>
<blockquote>
<p>Oh yeah, also having select marked <code>#[inline]</code> helped a bit during some tests, which it currently is not. I had the code hacked up a lot when I was doing early testing and it was necessary to avoid a call to select. Since we implement it directly on the <code>$mask</code> I think it's necessary.</p>
</blockquote>
<p>Yeah, Rust needs you to use <code>#[inline]</code> everywhere right now for good results inside a SIMD function because uhhh the compiler doesn't understand registers.</p>



<a name="236592716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236592716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236592716">(Apr 28 2021 at 22:54)</a>:</h4>
<p>Lol! Yeah, that definitely seems to be the case. Anything with a generic automatically includes the IR information for it to treat it as if it's inline thought right?</p>



<a name="236593300"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236593300" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236593300">(Apr 28 2021 at 23:00)</a>:</h4>
<p>That's true but if that helps codegen is a different story</p>



<a name="236593345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236593345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236593345">(Apr 28 2021 at 23:01)</a>:</h4>
<p>The inline attribute tells llvm that you'd rather it inline the function than not as well</p>



<a name="236597172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236597172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236597172">(Apr 28 2021 at 23:43)</a>:</h4>
<p>So the plan is to change the internals of the bitmask to a <code>[u8; X]</code>? Which will also solve the future &gt;512-bit concerns? Or did I misread</p>



<a name="236599156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236599156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236599156">(Apr 29 2021 at 00:06)</a>:</h4>
<p>Yeah, that is the plan</p>



<a name="236599224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236599224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236599224">(Apr 29 2021 at 00:06)</a>:</h4>
<p>I'm going to take a look right now</p>



<a name="236599930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236599930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236599930">(Apr 29 2021 at 00:15)</a>:</h4>
<p>You think this fixes the <code>to_int()</code> stuff? What's the advantage to the array?</p>



<a name="236600348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236600348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236600348">(Apr 29 2021 at 00:20)</a>:</h4>
<p>The reasoning for the array is that it supports much larger lane counts</p>



<a name="236606667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236606667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236606667">(Apr 29 2021 at 01:45)</a>:</h4>
<p>an array also supports non-power-of-2 lane counts</p>



<a name="236606831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236606831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236606831">(Apr 29 2021 at 01:47)</a>:</h4>
<p>though, for SimpleV at the ISA level, we just throw everything in a 64-bit integer (or a vector of condition registers)</p>



<a name="236609758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236609758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236609758">(Apr 29 2021 at 02:26)</a>:</h4>
<p>I got bitmasks "working" with a suggestion that looked similar to <span class="user-mention" data-user-id="229517">@Jacob Lifshay</span>'s suggestion.  It uses a <code>Mask</code> trait that has an associated type.  Fortunately that associated type can be generated with <code>const_evaluatable_unchecked</code> in the future, so only the <code>Mask</code> trait bleeds into the interface, which isn't too bad</p>



<a name="236613267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236613267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236613267">(Apr 29 2021 at 03:23)</a>:</h4>
<p><span class="user-mention" data-user-id="356799">@Brandon Falk</span> I just updated the <code>feature/select</code> branch if you want to give it a shot and let me know how well it optimizes.  It uses your <code>simd_select_bitmask</code> trick, but it has a few <code>transmute_copy</code> to workaround some of our intrinsic limitations.  I'm hoping they don't get in the way of any optimizations.</p>



<a name="236732475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236732475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236732475">(Apr 29 2021 at 19:36)</a>:</h4>
<p><span class="user-mention" data-user-id="312331">@Caleb Zulawski</span> Code gen looks fantastic! I am getting a few build warnings with my basic setup.</p>
<div class="codehilite" data-code-language="Text only"><pre><span></span><code>pleb@polar ~/stdsimd_test $ cargo build --release
warning: unused macro definition
   --&gt; /home/pleb/stdsimd/crates/core_simd/src/reduction.rs:105:1
    |
105 | / macro_rules! impl_full_mask_reductions {
106 | |     { $name:ident, $bits_ty:ident } =&gt; {
107 | |         impl&lt;T: crate::Mask, const LANES: usize&gt; $name&lt;T, LANES&gt;
108 | |         where
...   |
121 | |     }
122 | | }
    | |_^
    |
    = note: `#[warn(unused_macros)]` on by default

warning: function is never used: `simd_reduce_all`
  --&gt; /home/pleb/stdsimd/crates/core_simd/src/intrinsics.rs:72:5
   |
72 |     pub(crate) fn simd_reduce_all&lt;T&gt;(x: T) -&gt; bool;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: function is never used: `simd_reduce_any`
  --&gt; /home/pleb/stdsimd/crates/core_simd/src/intrinsics.rs:73:5
   |
73 |     pub(crate) fn simd_reduce_any&lt;T&gt;(x: T) -&gt; bool;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: 3 warnings emitted

   Compiling stdsimd_test v0.1.0 (/home/pleb/stdsimd_test)
    Finished release [optimized] target(s) in 0.14s
</code></pre></div>



<a name="236732581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236732581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236732581">(Apr 29 2021 at 19:37)</a>:</h4>
<p>well those are just unused warnings</p>



<a name="236732698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236732698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236732698">(Apr 29 2021 at 19:38)</a>:</h4>
<p>Yeah, the logic on when they're used is relatively complicated too, I should probably just slap allow(unused) on those</p>



<a name="236732714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236732714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236732714">(Apr 29 2021 at 19:38)</a>:</h4>
<p>Glad the codegen looks good</p>



<a name="236732758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236732758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236732758">(Apr 29 2021 at 19:38)</a>:</h4>
<p>I was slightly concerned the transmute_copys would turn into memcpys</p>



<a name="236736050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236736050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236736050">(Apr 29 2021 at 20:02)</a>:</h4>
<p>Yeah, those look like they come through clean. I hopefully will have time today to work on my more complex use of stdsimd which will stress some of these things. The only place where I start to get concerned about things like transmute copy is when they affect the thresholds for inlining, or other unrolling optimizations. (Eg. by having more stuff going on, the compiler might not inline enough stuff to DCE it out). But, I suspect the copies will never be a problem</p>



<a name="236736219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236736219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236736219">(Apr 29 2021 at 20:03)</a>:</h4>
<p>Definitely going to play around a lot with it and give feedback. stdsimd is arguably the best way to write vectorized code, especially arch agnostic, so I hope to put a decent amount of time into it (eg. scatters and gathers), as I think it'll be my standard HPC dev env in a few months!</p>



<a name="236736838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236736838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236736838">(Apr 29 2021 at 20:07)</a>:</h4>
<p>Also, slight side note. Does anyone understand how Rust calling conventions work with SIMD (specifically 512-bit). The code generation is so bad compared to <code>extern</code>. <a href="https://rust.godbolt.org/z/WMhrrT834">https://rust.godbolt.org/z/WMhrrT834</a> . It seems Rust calling convention requires them to be passed in memory? :(</p>



<a name="236739963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236739963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236739963">(Apr 29 2021 at 20:29)</a>:</h4>
<p>They are passed by reference for safety.  The reason is because the caller and callee may have different target feature sets which could result in different calling conventions</p>



<a name="236740231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236740231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Caleb Zulawski <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236740231">(Apr 29 2021 at 20:30)</a>:</h4>
<p>The idea of including the feature set in the ABI has been floated around but I think it's a large change without much support behind it</p>



<a name="236741297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257879-project-portable-simd/topic/Masks/near/236741297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brandon Falk <a href="https://rust-lang.github.io/zulip_archive/stream/257879-project-portable-simd/topic/Masks.html#236741297">(Apr 29 2021 at 20:38)</a>:</h4>
<p>Ah I see. Probably not a huge issue as with inlining and LTO prologues and epiloges shoudln't be a major issue anyways</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>